/**
 * 
 */
package es.ubu.ceudaaj.exercise.interfaz;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.DefaultListModel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JList;

import es.ubu.ceudaaj.exercise.ud02.t04.l03.Agenda;
import es.ubu.ceudaaj.exercise.ud02.t04.l03.AgendaJAXB;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @copirygth
 * @author Miguel
 * @version
 * 
 */
public class BuscarJAXB extends JFrame {
	private Agenda agenda;
	private JPanel contentPane;
	private JPanel tpBuscarCPostal;
	private JPanel tpBuscarCargo;
	private JPanel tpBuscarNombre;
	private JTabbedPane tpBuscar;
	private JLabel lblNombre;
	private JTextField tfNombre;
	private JButton btnAceptar;
	private JButton btnCancelar;
	private String nombre;
	private String cargo;
	private String cpostal;
	private JList listNombres;
	private JLabel lblCargo;
	private JTextField tfCargo;
	private JButton btnAceptarCargo;
	private JButton btnCancelarCargo;
	private JList listCargos;
	private JTextField tfCPostal;
	private JList listCPostal;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BuscarJAXB frame = new BuscarJAXB();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BuscarJAXB() {
		agenda = AgendaJAXB.getInstance();

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		tpBuscar = new JTabbedPane();
		tpBuscar.setBounds(10, 11, 414, 240);

		tpBuscarNombre = new JPanel();
		tpBuscar.addTab("Buscar por Nombre", tpBuscarNombre);
		GridBagLayout gbl_tpBuscarNombre = new GridBagLayout();
		gbl_tpBuscarNombre.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_tpBuscarNombre.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		gbl_tpBuscarNombre.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0,
				0.0, 0.0, 1.0, Double.MIN_VALUE };
		gbl_tpBuscarNombre.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0, 0.0,
				0.0, Double.MIN_VALUE };
		tpBuscarNombre.setLayout(gbl_tpBuscarNombre);

		lblNombre = new JLabel("Nombre del contacto buscado: ");
		GridBagConstraints gbc_lblNombre = new GridBagConstraints();
		gbc_lblNombre.insets = new Insets(0, 0, 5, 5);
		gbc_lblNombre.gridx = 1;
		gbc_lblNombre.gridy = 1;
		tpBuscarNombre.add(lblNombre, gbc_lblNombre);

		tfNombre = new JTextField();
		GridBagConstraints gbc_tfNombre = new GridBagConstraints();
		gbc_tfNombre.insets = new Insets(0, 0, 5, 5);
		gbc_tfNombre.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfNombre.gridx = 4;
		gbc_tfNombre.gridy = 1;
		tpBuscarNombre.add(tfNombre, gbc_tfNombre);
		tfNombre.setColumns(10);

		listNombres = new JList();
		GridBagConstraints gbc_listNombres = new GridBagConstraints();
		gbc_listNombres.gridwidth = 4;
		gbc_listNombres.insets = new Insets(0, 0, 5, 5);
		gbc_listNombres.fill = GridBagConstraints.BOTH;
		gbc_listNombres.gridx = 1;
		gbc_listNombres.gridy = 3;
		tpBuscarNombre.add(listNombres, gbc_listNombres);

		btnAceptar = new JButton("Aceptar");
		btnAceptar.addMouseListener(new BtnAceptarMouseListener());
		GridBagConstraints gbc_btnAceptar = new GridBagConstraints();
		gbc_btnAceptar.insets = new Insets(0, 0, 0, 5);
		gbc_btnAceptar.gridx = 1;
		gbc_btnAceptar.gridy = 5;
		tpBuscarNombre.add(btnAceptar, gbc_btnAceptar);

		btnCancelar = new JButton("Cancelar");

		btnCancelar.addMouseListener(new BtnCancelarMouseListener());
		GridBagConstraints gbc_btnCancelar = new GridBagConstraints();
		gbc_btnCancelar.insets = new Insets(0, 0, 0, 5);
		gbc_btnCancelar.gridx = 4;
		gbc_btnCancelar.gridy = 5;
		tpBuscarNombre.add(btnCancelar, gbc_btnCancelar);
		tpBuscarCargo = new JPanel();
		tpBuscar.addTab("Buscar por Cargo", tpBuscarCargo);
		GridBagLayout gbl_tpBuscarCargo = new GridBagLayout();
		gbl_tpBuscarCargo.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		gbl_tpBuscarCargo.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_tpBuscarCargo.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0,
				1.0, 0.0, Double.MIN_VALUE };
		gbl_tpBuscarCargo.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0, 0.0,
				0.0, 0.0, Double.MIN_VALUE };
		tpBuscarCargo.setLayout(gbl_tpBuscarCargo);

		lblCargo = new JLabel("Cargo del contacto buscado: ");
		GridBagConstraints gbc_lblCargo = new GridBagConstraints();
		gbc_lblCargo.insets = new Insets(0, 0, 5, 5);
		gbc_lblCargo.gridx = 1;
		gbc_lblCargo.gridy = 1;
		tpBuscarCargo.add(lblCargo, gbc_lblCargo);

		tfCargo = new JTextField();
		GridBagConstraints gbc_tfCargo = new GridBagConstraints();
		gbc_tfCargo.insets = new Insets(0, 0, 5, 5);
		gbc_tfCargo.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfCargo.gridx = 4;
		gbc_tfCargo.gridy = 1;
		tpBuscarCargo.add(tfCargo, gbc_tfCargo);
		tfCargo.setColumns(10);

		listCargos = new JList();
		GridBagConstraints gbc_listCargos = new GridBagConstraints();
		gbc_listCargos.gridheight = 3;
		gbc_listCargos.gridwidth = 4;
		gbc_listCargos.insets = new Insets(0, 0, 5, 5);
		gbc_listCargos.fill = GridBagConstraints.BOTH;
		gbc_listCargos.gridx = 1;
		gbc_listCargos.gridy = 3;
		tpBuscarCargo.add(listCargos, gbc_listCargos);

		btnAceptarCargo = new JButton("Aceptar");
		btnAceptarCargo.addMouseListener(new BtnAceptarCargoMouseListener());
		GridBagConstraints gbc_btnAceptarCargo = new GridBagConstraints();
		gbc_btnAceptarCargo.insets = new Insets(0, 0, 0, 5);
		gbc_btnAceptarCargo.gridx = 1;
		gbc_btnAceptarCargo.gridy = 6;
		tpBuscarCargo.add(btnAceptarCargo, gbc_btnAceptarCargo);

		btnCancelarCargo = new JButton("Cancelar");
		btnCancelarCargo.addMouseListener(new BtnCancelarCargoMouseListener());
		GridBagConstraints gbc_btnCancelarCargo = new GridBagConstraints();
		gbc_btnCancelarCargo.insets = new Insets(0, 0, 0, 5);
		gbc_btnCancelarCargo.gridx = 4;
		gbc_btnCancelarCargo.gridy = 6;
		tpBuscarCargo.add(btnCancelarCargo, gbc_btnCancelarCargo);
		tpBuscarCPostal = new JPanel();
		tpBuscar.addTab("Buscar por C.Postal", tpBuscarCPostal);
		GridBagLayout gbl_tpBuscarCPostal = new GridBagLayout();
		gbl_tpBuscarCPostal.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_tpBuscarCPostal.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_tpBuscarCPostal.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0,
				0.0, 1.0, 0.0, Double.MIN_VALUE };
		gbl_tpBuscarCPostal.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
				0.0, 0.0, 0.0, Double.MIN_VALUE };
		tpBuscarCPostal.setLayout(gbl_tpBuscarCPostal);

		JLabel lblCPostal = new JLabel("C.Postal del contacto buscado:");
		GridBagConstraints gbc_lblCPostal = new GridBagConstraints();
		gbc_lblCPostal.insets = new Insets(0, 0, 5, 5);
		gbc_lblCPostal.gridx = 1;
		gbc_lblCPostal.gridy = 1;
		tpBuscarCPostal.add(lblCPostal, gbc_lblCPostal);

		tfCPostal = new JTextField();
		GridBagConstraints gbc_tfCPostal = new GridBagConstraints();
		gbc_tfCPostal.anchor = GridBagConstraints.NORTH;
		gbc_tfCPostal.insets = new Insets(0, 0, 5, 5);
		gbc_tfCPostal.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfCPostal.gridx = 5;
		gbc_tfCPostal.gridy = 1;
		tpBuscarCPostal.add(tfCPostal, gbc_tfCPostal);
		tfCPostal.setColumns(10);

		listCPostal = new JList();
		GridBagConstraints gbc_listCPostal = new GridBagConstraints();
		gbc_listCPostal.gridheight = 4;
		gbc_listCPostal.gridwidth = 5;
		gbc_listCPostal.insets = new Insets(0, 0, 5, 5);
		gbc_listCPostal.fill = GridBagConstraints.BOTH;
		gbc_listCPostal.gridx = 1;
		gbc_listCPostal.gridy = 2;
		tpBuscarCPostal.add(listCPostal, gbc_listCPostal);

		JButton btnAceptarCPostal = new JButton("Aceptar");
		btnAceptarCPostal
				.addMouseListener(new BtnAceptarCPostalMouseListener());
		GridBagConstraints gbc_btnAceptarCPostal = new GridBagConstraints();
		gbc_btnAceptarCPostal.insets = new Insets(0, 0, 0, 5);
		gbc_btnAceptarCPostal.gridx = 1;
		gbc_btnAceptarCPostal.gridy = 6;
		tpBuscarCPostal.add(btnAceptarCPostal, gbc_btnAceptarCPostal);

		JButton btnCancelarCPostal = new JButton("Cancelar");
		btnCancelarCPostal
				.addMouseListener(new BtnCancelarCPostalMouseListener());
		GridBagConstraints gbc_btnCancelarCPostal = new GridBagConstraints();
		gbc_btnCancelarCPostal.insets = new Insets(0, 0, 0, 5);
		gbc_btnCancelarCPostal.gridx = 5;
		gbc_btnCancelarCPostal.gridy = 6;
		tpBuscarCPostal.add(btnCancelarCPostal, gbc_btnCancelarCPostal);
		contentPane.add(tpBuscar);
	}

	private class BtnCancelarMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			dispose();
		}
	}

	private class BtnAceptarMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent arg0) {
			List<String> results = new ArrayList<String>();
			DefaultListModel modelo = new DefaultListModel();
			nombre = tfNombre.getText();

			results = agenda.searchByName(nombre);

			for (int i = 0; i < results.size(); i++) {

				modelo.addElement(results.get(i));
			}

			listNombres.setModel(modelo);

		}
	}

	private class BtnCancelarCargoMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			dispose();
		}
	}

	private class BtnAceptarCargoMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			List<String> results = new ArrayList<String>();
			DefaultListModel modelo = new DefaultListModel();
			cargo = tfCargo.getText();

			results = agenda.searchByJob(cargo);

			for (int i = 0; i < results.size(); i++) {

				modelo.addElement(results.get(i));
			}

			listCargos.setModel(modelo);
		}
	}

	private class BtnCancelarCPostalMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			dispose();
		}
	}

	private class BtnAceptarCPostalMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			List<String> results = new ArrayList<String>();
			DefaultListModel modelo = new DefaultListModel();
			cpostal = tfCPostal.getText();

			results = agenda.searchByPostalCode(cpostal);

			for (int i = 0; i < results.size(); i++) {

				modelo.addElement(results.get(i));
			}

			listCPostal.setModel(modelo);
		}
	}
}
